/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package connection;

import gov.nist.siplite.message.Message;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.wireless.messaging.BinaryMessage;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

public class ReceiveSMS implements Runnable {

    private Vector mensagens;
    private boolean newMessage;
    
    public ReceiveSMS() {
        this.newMessage = false;
        mensagens = new Vector();
        Thread sms = new Thread(this);
        sms.start();

    }

    public void run() {

        notifyIncomingMessage();

    }

    public void notifyIncomingMessage() {

        MessageConnection conn = null;
        try {
            conn = (MessageConnection) Connector.open("sms://:5000");
            //conn.setMessageListener(MessageListener ml);
        } catch (IOException ex) {
            System.out.println("dei boxe");
            ex.printStackTrace();
        }
        String msgReceived = "";

        Message msg = null;
        //  Try reading (maybe block for) a message
        try {
            msg = (Message) conn.receive();
        } catch (Exception e) {
            // Handle reading errors
            System.out.println("processMessage.receive " + e);
        }
        // Process the received message
        if (msg instanceof TextMessage) {
            TextMessage tmsg = (TextMessage) msg;
            msgReceived = tmsg.getPayloadText();
            //System.out.println("yoyoyo " + msgReceived);
        } else {
            // process received message
            if (msg instanceof BinaryMessage) {
                BinaryMessage bmsg = (BinaryMessage) msg;
                byte[] data = bmsg.getPayloadData();
                //  Handle the binary message...
           //     msgReceived = data.toString();
            //    System.out.println("yoyoyo " + msgReceived);
            }
        }
        this.mensagens.addElement(msgReceived);
        this.newMessage = true;
        
    }

    public String getPuta(){
        System.out.println("\t\ttamanho: "+mensagens.size());
        return "Puta";
    }
    
    
    public Vector getMensagens() {
        return mensagens;
    }

    public void setMensagens(Vector mensagens) {
        this.mensagens = mensagens;
    }

    public boolean isNewAlert() {
        return newMessage;
    }

    public void setNewAlert(boolean newMessage) {
        this.newMessage = newMessage;
    }
    
    
}